package com.hyphenate.easeui.utils;

import android.content.Context;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.iwinding.ladygg.R;
import com.hyphenate.easeui.controller.EaseUI;
import com.hyphenate.easeui.controller.EaseUI.EaseUserProfileProvider;
import com.hyphenate.easeui.domain.EaseUser;

public class EaseUserUtils {
    
    static EaseUserProfileProvider userProvider;
    
    static {
        userProvider = EaseUI.getInstance().getUserProfileProvider();
    }
    
    /**
     * get EaseUser according username
     * @param username
     * @return
     */
    public static EaseUser getUserInfo(String username){
        if(userProvider != null)
            return userProvider.getUser(username);
        
        return null;
    }
    
    /**
     * set user avatar
     * @param username
     */
    public static void setUserAvatar(Context context, String username, ImageView imageView){
    	EaseUser user = getUserInfo(username);
        if(user != null && user.getAvatar() != null){
            try {
                int avatarResId = Integer.parseInt(user.getAvatar());
                Glide.with(context).load(avatarResId).into(imageView);
            } catch (Exception e) {
                //use default avatar
                Glide.with(context).load(user.getAvatar()).diskCacheStrategy(DiskCacheStrategy.ALL).placeholder(R.drawable.ease_default_avatar).into(imageView);
            }
        }else{
            Glide.with(context).load(R.drawable.ease_default_avatar).into(imageView);
        }
    }
    
    /**
     * set user's nickname
     */
    public static void setUserNick(String username,TextView textView){
        if(textView != null){
        	EaseUser user = getUserInfo(username);
        	if(user != null && user.getNick() != null){
        		textView.setText(user.getNick());
        	}else{
        		textView.setText(username);
        	}
        }
    }


//    public static String getICaneUserName(Context mContext,String groupId){
//        final ICaneApplication iCaneApplication = ((ICaneApplication) mContext.getApplicationContext());
//        final LoginRp loginRp = iCaneApplication.getmLoginRp();
//        if(loginRp==null){
//            mContext.startActivity(new Intent(mContext, LoginActivity.class));
//            if(mContext instanceof MainActivity){
//                ((MainActivity) mContext).finish();
//            }
//        }
//        EaseUser user = getUserInfo(groupId);
//        String loginGroupId = loginRp.groupId;
//        String name = user.getNick();
//        if (!TextUtils.isEmpty(loginGroupId) && TextUtils.equals(loginRp.groupId, groupId)) {
//            name = "我的SOS";
//        } else {
//            EaseUser users = SOSUserProfileProvider.getInstance().queryUserByGroupId(mContext,groupId);
//            if (users == null) {
//                name = "未知";
//            } else {
//                name = users.getNick() + "的SOS";
//            }
//        }
//        return name;
//    }
    
}
